<?php
/**
 * 功能: 展示教学
 * User: WD
 * Date: 2018/12/28 0028
 * Time: 14:30
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Reveal extends Power{
    /**
     * 展示教学 - 列表
     */
    public function index(){
        $reveal = Db::name('reveal')
            ->paginate(10)
            ->each(function($item,$key){
                $item['type_name'] = Db::name('reveal_type')->where('id',$item['type'])->value('name');
                return $item;
            });
        $count  = Db::name('reveal')->count();

        $this->assign([
            'reveal'=>$reveal,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="6">暂无数据</td></tr>'
        ]);
        return view('');
    }

    /**
     *  添加 /修改页面
     */
    public function reveal_add(){
        $id = intval(input('id'));
        if($id){
            $res = Db::name('reveal')->where('id',$id)->find();
            $res['images'] = array_filter(explode(',',$res['images']));
            $this->assign(['res'=>$res]);
        }
        $type =  Db::name('reveal_type')->select();
        $this->assign(['type'=>$type]);
        return view('');
    }
    public function reveal_hold(){
        $title   = trim(input('title'));
        $type    = input('type');
        $images  = input('images');
        $intro   = input('intro');
        $content = input('content');
        if(empty($title)||empty($intro)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'type'=>$type,
            'images'=>$images,
            'intro'=>$intro,
            'content'=>$content
        ];
        $res = Db::name('reveal')->insert($data);
        if($res){
            return json(['msg'=>'添加成功','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败','ok'=>false]);
        }

    }

    /**
     * 修改
     */
    public function reveal_edit(){
        $id      = intval(input('reveal_id'));
       /* dump($id);
        die;*/
        $title   = trim(input('title'));
        $type    = input('type');
        $images  = input('images');
        $intro   = input('intro');
        $content = input('content');
        if(empty($title)||empty($intro)){
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        $data = [
            'title'=>$title,
            'type'=>$type,
            'images'=>$images,
            'intro'=>$intro,
            'content'=>$content
        ];
        try{
            $res = Db::name('reveal')->where('id',$id)->update($data);
            return json(['msg'=>'修改成功','ok'=>true]);
        }catch (\Exception $e){
            $err = $e->getMessage();
            return json_encode(['msg'=>$err,'ok'=>false]);
        }

        /*if($res){
            return json(['msg'=>'修改成功','ok'=>true]);
        }else{
            return json(['msg'=>'您未作出修改','ok'=>false]);
        }*/
    }

}